<!doctype html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover"/>
    <meta http-equiv="X-UA-Compatible" content="ie=edge"/>
    <link rel="preconnect" href="https://fonts.gstatic.com/" crossorigin>
    <meta name="msapplication-TileColor" content="#206bc4"/>
    <meta name="theme-color" content="#206bc4"/>
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"/>
    <meta name="apple-mobile-web-app-capable" content="yes"/>
    <meta name="mobile-web-app-capable" content="yes"/>
    <meta name="HandheldFriendly" content="True"/>
    <meta name="MobileOptimized" content="320"/>
    <meta name="robots" content="noindex,nofollow,noarchive"/>
    <link rel="icon" href="./favicon.ico" type="image/x-icon"/>
    <link rel="shortcut icon" href="./favicon.ico" type="image/x-icon"/>
    <!-- CSS files -->
    <link href="/dist/css/tabler.min.css" rel="stylesheet"/>
    <link href="/dist/css/demo.min.css" rel="stylesheet"/>
    <link href="https://cdn.bootcdn.net/ajax/libs/zTree.v3/3.5.01/css/zTreeStyle/zTreeStyle.css" rel="stylesheet">
    <link href="/dist/css/metroStyle/metroStyle.css" rel="stylesheet"/>
    <style>
        body {
            display: none;
        }
    </style>
</head>
<body class="antialiased">
<div class="page">
    <div class="content">
        <div class="container-xl">
            <div class="row">
                <div class="col-12">
                    <div class="row" style="padding: 20px">
                        <div class="card-body">
                            <form>
                                <div class="form-group mb-3">
                                    <label class="form-label">角色名称</label>
                                    <div>
                                        <input type="text" class="form-control role-name"
                                               th:value="${data.roleName}">
                                    </div>
                                </div>
                                <div class="form-group mb-3">
                                    <label class="form-label">排序</label>
                                    <div>
                                        <input type="number" min="0" class="form-control role_sort"
                                               th:value="${data.sort}">
                                    </div>
                                </div>
                                <div class="form-group mb-3">
                                    <label class="form-label">备注</label>
                                    <div>
                                        <input type="text" class="form-control role_remark"
                                               th:value="${data.description}">
                                    </div>
                                </div>
                                <div class="form-group mb-3">
                                    <label class="form-label">创建时间</label>
                                    <div>
                                        <input type="text" class="form-control" readonly
                                               th:value="${#dates.format(data.createTime, 'yyyy-MM-dd HH:mm:ss')}">
                                    </div>
                                </div>
                                <div class="form-group mb-3">
                                    <label class="form-label">修改时间</label>
                                    <div>
                                        <input type="text" class="form-control" readonly
                                               th:value="${#dates.format(data.updateTime, 'yyyy-MM-dd HH:mm:ss')}">
                                    </div>
                                </div>
                                <div class="form-group mb-3">
                                    <label class="form-label required">菜单权限</label>
                                    <ul id="menuPermission" class="ztree"></ul>
                                </div>
                                <div class="form-footer">
                                    <button type="button" class="btn btn-primary formCommit">提交</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
<!-- Libs JS -->
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script src="/dist/libs/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/zTree.v3/3.5.01/js/jquery.ztree.all.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/layer/3.1.1/layer.min.js"></script>
<!-- Tabler Core -->
<script src="/dist/js/tabler.min.js"></script>
<!--<script src="/dist/js/role/updateRole.js"></script>-->
<script>
    var roleId = '[[${data.id}]]';
    document.body.style.display = "block";
    var setting = {
        check: {
            enable: true,
            chkStyle: "checkbox",
            chkboxType:  { "Y" : "p", "N" : "s" }
        }, data: {
            simpleData: {
                enable: true
            }
        }
    };
    var tree;
    $(document).ready(function () {
        var permissionIds = [[${data.permissionIds}]];

        var data = ["D", "M", "B"];
        var treeData = null;
        $.ajax({
            type: "POST",
            url: "/menu/getZtreeMenu",
            async: false,
            dataType: 'json',
            contentType: "application/json",
            data: JSON.stringify(data),
            success: function (res) {
                treeData = res;
            }
        });
        tree = $.fn.zTree.init($("#menuPermission"), setting, treeData);
        tree.expandAll(true);
        if (1 == roleId) {
            tree.checkAllNodes(true);
            return;
        }

        var nodes = tree.transformToArray(tree.getNodes());
        for (var i = 0; i < nodes.length; i++) {
            for (var j = 0; j < permissionIds.length; j++) {
                if (nodes[i].id == permissionIds[j]) {
                    console.log(nodes[i].id)
                    tree.checkNode(nodes[i], true, true);
                }
            }
        }

    });

    $(".formCommit").click(function () {
        treeChecked = tree.getCheckedNodes();
        if (treeChecked.length == 0) {
            layer.msg("请选择归属关系!");
            return;
        }
        let arr = [];
        for (var i = 0; i < treeChecked.length; i++) {
            arr.push(treeChecked[i].id)
        }
        console.log(arr)
        var data = {
            "id": roleId,
            "roleName": $(".role-name").val(),
            "sort": $(".role_sort").val(),
            "remark": $(".role_remark").val(),
            "ids": arr
        };

        $.ajax({
            type: "POST",
            url: '/role/updateRole',
            dataType: 'json',
            contentType: 'application/json',
            data: JSON.stringify(data),
            success: function (res) {
                if (res.code == 1) {
                    window.parent.location.reload();

                } else {
                    layer.msg(res.message);
                }
            },
            error: function (res) {
                layer.msg("失败");
            }
        });
    });

</script>
</body>
</html>
